package cn.mumzone.pos.ui.fragment.member;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.widget.NestedScrollView;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;

import butterknife.BindView;
import cn.mumzone.basic.adapter.rv.CommonAdapter;
import cn.mumzone.basic.base.BaseFragment;
import cn.mumzone.basic.util.ListUtil;
import cn.mumzone.basic.util.PosToast;
import cn.mumzone.basic.util.StringUtils;
import cn.mumzone.pos.AppConstants;
import cn.mumzone.pos.R;
import cn.mumzone.pos.model.MemberTicketEntity;
import cn.mumzone.pos.presenter.CouponPresenter;
import cn.mumzone.pos.presenter.contract.CouponContract;
import cn.mumzone.pos.ui.adapter.ADA_MemberCouponList;
import cn.mumzone.pos.ui.widgets.others.StoreHouseHeader;
import cn.mumzone.pos.ui.widgets.refresh.SmartRefreshLayout;
import cn.mumzone.pos.ui.widgets.refresh.api.RefreshLayout;
import cn.mumzone.pos.ui.widgets.refresh.listener.OnLoadMoreListener;
import cn.mumzone.pos.ui.widgets.refresh.listener.OnRefreshListener;

public class FRA_Coupon extends BaseFragment<CouponPresenter> implements CouponContract.View {

    @BindView(R.id.rlv_unused)
    RecyclerView rlvUnused;
    @BindView(R.id.rlv_expired)
    RecyclerView rlvExpired;
    @BindView(R.id.no_ticket_layout)
    LinearLayout noTicketLayout;
    private String memberId;

    @BindView(R.id.smart_refresh_layout)
    SmartRefreshLayout smartRefreshLayout;

    @BindView(R.id.ns_have_times_card)
    NestedScrollView nsHaveTimesCard;

    private int pageNo = 1;
    private ADA_MemberCouponList expiredCouponAdapter;

    @Override
    protected void getBundleExtras(Bundle extras) {
        super.getBundleExtras(extras);
        memberId = extras.getString("memberId");
    }


    @Override
    protected void onUserVisible() {
    }

    @Override
    protected void onFirstUserVisible() {
        super.onFirstUserVisible();
        smartRefreshLayout.autoRefresh();
    }


    @Override
    protected void initEventAndData() {
        smartRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                pageNo = 1;
                mPresenter.getTicketAllListPage(memberId, String.valueOf(1), String.valueOf(pageNo),
                        StringUtils.get(AppConstants.PAGE_SIZE), false);
            }
        });
        smartRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                pageNo++;
                mPresenter.getTicketAllListPage(memberId, String.valueOf(1), String.valueOf(pageNo),
                        StringUtils.get(AppConstants.PAGE_SIZE), true);
            }
        });
    }

    @Override
    protected int getLayout() {
        return R.layout.fra_coupon;
    }

    @Override
    public void setTicket(final MemberTicketEntity ticket) {
        if (ticket == null || (ticket.getOutTicketList() == null && ticket.getTicketList() == null) || (ticket.getTicketList().isEmpty() && ticket.getOutTicketList().isEmpty())) {
            nsHaveTimesCard.setVisibility(View.GONE);
            noTicketLayout.setVisibility(View.VISIBLE);
        } else {
            noTicketLayout.setVisibility(View.GONE);
            nsHaveTimesCard.setVisibility(View.VISIBLE);

            rlvUnused.setLayoutManager(new LinearLayoutManager(getActivity()));
            rlvUnused.setNestedScrollingEnabled(false);
            ADA_MemberCouponList unusedCouponAdapter = new ADA_MemberCouponList(getActivity());
            rlvUnused.setAdapter(unusedCouponAdapter);
            unusedCouponAdapter.setDataList(ticket.getTicketList());

            rlvExpired.setLayoutManager(new LinearLayoutManager(getActivity()));
            rlvExpired.setNestedScrollingEnabled(false);
            expiredCouponAdapter = new ADA_MemberCouponList(getActivity());
            rlvExpired.setAdapter(expiredCouponAdapter);
            expiredCouponAdapter.setDataList(ticket.getOutTicketList());
        }
    }

    @Override
    public void appendTicket(MemberTicketEntity memberTicketEntity) {
        smartRefreshLayout.finishLoadMore();
        if (memberTicketEntity != null && memberTicketEntity.getOutTicketList() != null && !memberTicketEntity.getOutTicketList().isEmpty()) {
            expiredCouponAdapter.update(memberTicketEntity.getOutTicketList(), false);
        }
    }

    @Override
    public void dismissLoading() {
        smartRefreshLayout.finishRefresh();
        smartRefreshLayout.finishLoadMore();
    }

    @Override
    protected View getTargetView() {
        return smartRefreshLayout;
    }

    @Override
    protected void refreshNetwork() {
        smartRefreshLayout.autoRefresh();
    }

    @Override
    public void showNetError() {
        smartRefreshLayout.finishRefresh();

        if (rlvUnused == null || rlvExpired == null){
            return;
        }

        CommonAdapter unUsedAdapter = (CommonAdapter) rlvUnused.getAdapter();
        CommonAdapter expiredAdapter = (CommonAdapter) rlvExpired.getAdapter();

        if (ListUtil.isListEmpty(unUsedAdapter.getDataList())
                && ListUtil.isListEmpty(expiredAdapter.getDataList())) {
            super.showNetError();
        } else {
            PosToast.INSTANCE.makeShortToast(mContext, "网络不给力，请稍后重试");
        }
    }

}
